package com.question.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.question.Design.SingletonMode;
import com.question.dao.IPlayerDao;
import com.question.entity.Answer;
import com.question.entity.Question;
import com.question.entity.User;
import com.question.service.IPlayerService;
import com.question.utils.DataSourceUtils;

public class PlayerServiceImpl implements IPlayerService{
	private IPlayerDao playDao = SingletonMode.getPlayDao();
	@Override
	public List<Question> questionList(Question question) {
		List<Question> questionList = null;
		try {
			questionList = playDao.questionList(question);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return questionList;
	}
	@Override
	public Question getQuestionById(Integer id) {
		Question question = null;
		try {
			question = playDao.getQuestionById(id);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return question;
	}
	@Override
	public void answerQuestion(Answer answer) {
		try {
			playDao.answerQuestion(answer);
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void askQuestion(Question question) {
		try {
			DataSourceUtils.getConnection();
			DataSourceUtils.startTransaction();
			playDao.askQuestion(question);
			question.getUser().setScore(question.getUser().getScore()-question.getCredit());
			playDao.editScoreByUser(question.getUser());
			DataSourceUtils.commitAndRelease();
		}catch(SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	@Override
	public List<Question> findQuestionByUser(User player) {
		List<Question> list = null;
		try {
			list = playDao.findQuestionByUser(player);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Answer> findAnswerByQuestionID(Integer inVal) {
		List<Answer> list = null;
		try {
			list = playDao.findAnswerByQuestionID(inVal);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Answer> isaccepted(User player, Integer qId) {
		List<Answer> list = null;
		try {
			list = playDao.findAnswerByQuestionIDAndPlayer(player,qId);
			if(list.size() == 0 || list.get(0).getAnswer_state() == 1 ) {
				list = null;
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public void acceptAnswer(Integer id,Integer qId) {
		try {
			DataSourceUtils.getConnection();
			DataSourceUtils.startTransaction();
			
			Question question = getQuestionById(qId);
			User user = playDao.findUserByAnswerId(id);
			user.setScore(user.getScore()+question.getCredit());
			user.setAnswer(user.getAnswer()+1);
			playDao.updateUserAccept(user);
			playDao.updateQuestionAccept(question);
			playDao.updateAnswerAccept(id);
			DataSourceUtils.commitAndRelease();
		}catch(SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	@Override
	public User updatePlayer(User player) {
		User user = null;
		try {
			user = playDao.updatePlayer(player); 
		}catch(Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	@Override
	public List<User> rankUserByScoreTop10() {
		List<User> list = null;
		try {
			list = playDao.rankUserByScoreTop10(); 
		}catch(Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public Question findQuestionInfoById(Integer id) {
		Question question = null;
		try {
			question = playDao.findQuestionInfoById(id);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return question;
	}
	
}
